<?php
	check_login();
	$a = getAction();

	switch($a) {
		case 'index':
			$name = trim($_GET['name']);
			$phone = trim($_GET['phone']);
			$sql = " select ct.*, broker.name as broker_name  from " . table('customer') . " ct "
				 . " left join " . table("broker") . " broker "
				 . " on broker.id = ct.broker_id"
				 . " where 1=1";
			$url="admin.php?m=cutomer";
			
			if(!empty($name))
			{
				$sql .=" and ct.name like '%".$name."%' ";
				$url.="&name={$name}";
			}
			
			if(!empty($phone))
			{
				$sql .=" and ct.phone like '%".$phone."%' ";
				$url.="&phone={$phone}";
			}
				
			$sql .=	" order by ct.id desc ";	

			$rscount=$db->getOne("select count(1) from ($sql) temp");
			$pagesize=20;
			$page=max(1,intval($_GET['page']));
			$start=($page-1)*$pagesize;
			$sql.=" limit $start,$pagesize";
			
			$list = $db->getAll($sql);

			for($i = 0; $i < count($list); $i++) {
				$list[$i]["gender_name"] = GENDER::getName($list[$i]["gender"]);
				$list[$i]["apartment_type_str"] = APARTMENT_TYPE::getName($list[$i]["apartment_type"]);
				if($list[$i]["acreage1"] || $list[$i]["acreage2"]) {
					$list[$i]["acreage_str"] = $list[$i]["acreage1"] . "～" . $list[$i]["acreage2"];
				}
				if($list[$i]["hope_price1"] || $list[$i]["hope_price2"]) {
					$list[$i]["hope_price_str"] = $list[$i]["hope_price1"] . "～" . $list[$i]["hope_price2"];
				}
				
				$list[$i]["status_name"] = CUST_STATUS::getCustStatusName($list[$i]["status"]);
				$list[$i]["callback_flag"] = $db->getOne("select count(1) from " . table("customer_callback") . " where customer_id = " . $list[$i]["id"]);

				if($list[$i]["building_id"]) {
					$list[$i]["building_name"] = $db->getOne("select name from " . table("building") . " where id = '" . $list[$i]["building_id"] . "'");
				}
			}
			
			$smarty->assign("pagelist",multipage($rscount,$pagesize,$page,$url));
			$smarty->assign("list", $list);

			$smarty->display("customer.html");
	
			break;	
		case "add":
			$id = get_post("id");
				
			if($_POST) {
				$formData = $_POST["formData"];
				$nowTime = getNowTime();
			
				if($id) {
					$formData["id"] = $id;
					$formData["update_time"] = $nowTime;
					$db->update("customer", $formData, " and id = $id ");
				} else {
					$formData["insert_time"] = $nowTime;
					$formData["update_time"] = $nowTime;
					$db->insert("customer", $formData);
				}

				gourl("admin.php?m=customer");
			} else {
				if($id) {
					$formData = $db->getRow("select * from " . table('customer') . " where id = $id ");
					$smarty->assign("formData", $formData);
				}
			
				$sql = " select * from " .table('building')
					 . " WHERE status = '" . STATUS::VALID . "' ";
				
				$smarty->assign("buildingList", $db->getAll($sql));
				
				$brokerList = $db->getAll("select * from " . table("broker") . " where isvalid = 1");
				$smarty->assign("brokerlist",$brokerList);
				$smarty->assign("apttypelist",APARTMENT_TYPE::getList());
				$smarty->display("customer_add.html");
			}
					
			break;	
		case "status":
			$id = get_post("id");
			if($_POST) {
				$formData = $_POST["formData"];
				$nowTime = getNowTime();
			
				$formData["id"] = $id;
				$formData["update_time"] = $nowTime;
				$db->update("customer", $formData, " and id = $id ");

				gourl("admin.php?m=customer");
			} else {
				$formData = $db->getRow("select * from " . table('customer') . " where id = $id ");
				$formData["broker_name"] = $db->getOne("select name from " . table("broker") . " where id = " . $formData["broker_id"]);
				$formData["apartment_name"] = APARTMENT_TYPE::getName($formData["apartment_type"]);
				$formData["gender_name"] = GENDER::getName($formData["gender"]);
				if($formData["acreage1"] || $formData["acreage2"]) {
					$formData["acreage_str"] = $formData["acreage1"] . "～" . $formData["acreage2"];
				}
				if($formData["hope_price1"] || $formData["hope_price2"]) {
					$formData["hope_price_str"] = $formData["hope_price1"] . "～" . $formData["hope_price2"];
				}
				
				$smarty->assign("formData", $formData);
				$smarty->display("customer_status.html");
			}
					
			break;	
		case 'contract':
			if($_POST) {
				$id=intval($_POST['id']);
				$formData["contract"] = $_POST["contract"];
				$formData["update_time"] = getNowTime();
				
				if($id)
				{
					$db->update("setting", $formData, " and id = $id");
				}else
				{
					$formData["insert_time"] = getNowTime();
					$db->insert("setting", $formData);
				}
								 
				errback("电子合同保存成功", "admin.php?m=broker&a=contract");
				exit();
			}
				
			$sql = " select *  from ".table('setting');
			$rs=$db->getRow($sql);
			$smarty->assign("formData", $rs);
			
			$smarty->display("broker_contract.html");
			break;
	
		case 'search':
			$name = trim($_GET['name']);
			$phone = trim($_GET['phone']);
			$sql = " select ct.*, broker.name as broker_name  from " . table('customer') . " ct "
				 . " left join " . table("broker") . " broker "
				 . " on broker.id = ct.broker_id"
				 . " where 1=1";
			$url="admin.php?m=cutomer&a=search";
			
			if(!empty($name))
			{
				$sql .=" and ct.name like '%".$name."%' ";
				$url.="&name={$name}";
			}
			
			if(!empty($phone))
			{
				$sql .=" and ct.phone like '%".$phone."%' ";
				$url.="&phone={$phone}";
			}
				
			$sql .=	" order by ct.id desc ";	

			$rscount=$db->getOne("select count(1) from ($sql) temp");
			$pagesize=20;
			$page=max(1,intval($_GET['page']));
			$start=($page-1)*$pagesize;
			$sql.=" limit $start,$pagesize";
			
			$list = $db->getAll($sql);

			for($i = 0; $i < count($list); $i++) {
				$list[$i]["gender_name"] = GENDER::getName($list[$i]["gender"]);
			}
			
			$smarty->assign("pagelist",multipage($rscount,$pagesize,$page,$url));
			$smarty->assign("list", $list);

			$smarty->display("customer_search.html");
	
			break;	
	}
	?>